// PairCalculatorDetail.cpp : implementation file
//

#include "stdafx.h"
#include "LightspeedTraderAPIDemo.h"
#include "PairCalculatorDetail.h"
#include "afxdialogex.h"


// PairCalculatorDetail dialog

IMPLEMENT_DYNAMIC(PairCalculatorDetail, CDialog)

PairCalculatorDetail::PairCalculatorDetail(PairTrade *tradePair, CWnd* pParent /*=NULL*/)
	: CDialog(PairCalculatorDetail::IDD, pParent)
	,fullQuote1(NULL)
	,fullQuote2(NULL)
	,m_tradePair_Old(tradePair)
	,m_tradePair_New(NULL)
{
	isFormClosing = false;
	if(tradePair == NULL)
	{
		IsNewTrade = true;
	}
	else
	{
		IsNewTrade = false;
	}
	m_CompareSymbol = 0;
	m_orderTradeType = 0;
}

PairCalculatorDetail::~PairCalculatorDetail()
{
	if (fullQuote1)
	{
		fullQuote1->L_Detach(this);
		L_DestroyFullQuote(fullQuote1);
		fullQuote1 = NULL;
	}
	if (fullQuote2)
	{
		fullQuote2->L_Detach(this);
		L_DestroyFullQuote(fullQuote2);
		fullQuote2 = NULL;
	}
}

BOOL PairCalculatorDetail::Create(CWnd *pParentWnd /*= CWnd::FromHandle(L_GetMainWnd())*/)
{
	return __super::Create(PairCalculatorDetail::IDD, pParentWnd);
}

void PairCalculatorDetail::DoDataExchange(CDataExchange* pDX)
{
	//CDialog::DoDataExchange(pDX);
	__super::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_txtRatio, ctrl_txtRatio);
	DDX_Control(pDX, IDC_cmbSym1Price, ctrl_cmbSymbol1PriceOn);
	DDX_Control(pDX, IDC_cmbSym2Price, ctrl_cmbSymbol2PriceOn);
	DDX_Control(pDX, IDC_SYMBOL1, m_editSymbol1);
	DDX_Control(pDX, IDC_SYMBOL2, m_editSymbol2);
	DDX_Control(pDX, lblSharesForSymbol1, m_lblSharesForSymbol1);
	DDX_Control(pDX, lblSharesForSymbol2, m_lblSharesForSymbol2);
	DDX_Control(pDX, CmbTradeOnSymbol1, m_DestinationComboSymbol1);
	DDX_Control(pDX, CmbTradeOnSymbol2, m_DestinationComboSymbol2);
	DDX_Control(pDX, IDOK, BtnOK);
	DDX_Radio(pDX, IDC_GT, m_CompareSymbol);
	DDV_MinMaxInt(pDX, m_CompareSymbol, 0, 3);
	DDX_Radio(pDX, rbTradeTypeBuy, m_orderTradeType);
	DDV_MinMaxInt(pDX, m_orderTradeType, 0, 1);
	DDX_Control(pDX, TxtSharesForSymbol1, m_txtSharesForSymbol1);
	DDX_Control(pDX, TxtSharesForSymbol2, m_txtSharesForSymbol2);
	DDX_Control(pDX, IDC_SYMBOL1Cash, m_Symbol1Cash);
	DDX_Control(pDX, IDC_SYMBOL2Cash, m_Symbol2Cash);
	DDX_Control(pDX, IDC_SYMBOLEDIT4, m_txtRequiredSpread);
	DDX_Control(pDX, CmbTradeAtSymbol1, m_cboOrderPriceOnSymbol1);
	DDX_Control(pDX, CmbTradeAtSymbol2, m_cboOrderPriceOnSymbol2);
	DDX_Control(pDX, IDC_lblActualSpread, m_lblActualSpred);
	DDX_Control(pDX, IDC_lblEvalution, m_lblEvalution);
	DDX_Control(pDX, lblTradeSummary, m_TradeSumarry);
}

BOOL PairCalculatorDetail::OnInitDialog()
{
	__super::OnInitDialog();
	m_tradePair_New = m_tradePair_Old;
	
	if(m_tradePair_New == NULL)
	{
		m_tradePair_New = new PairTrade();

		if(ctrl_cmbSymbol1PriceOn.GetCount() > 0)
		{
			ctrl_cmbSymbol1PriceOn.SetCurSel(0);
		}
		if(ctrl_cmbSymbol2PriceOn.GetCount() > 0)
		{
			ctrl_cmbSymbol2PriceOn.SetCurSel(1);
		}
		if(m_cboOrderPriceOnSymbol1.GetCount() > 0)
		{
			m_cboOrderPriceOnSymbol1.SetCurSel(0);
		}
		if(m_cboOrderPriceOnSymbol2.GetCount() > 0)
		{
			m_cboOrderPriceOnSymbol2.SetCurSel(1);
		}
	}
	else
	{
		CString ratio;
		CString strCashSymbol1;
		CString strCashSymbol2;
		CString strSharesSymbol1;
		CString strSharesSymbol2;
		CString strActualSpread;
		CString strRequiredSpread;

		ratio.Format("%.2f", m_tradePair_New->Ratio);
		ctrl_txtRatio.SetWindowTextA(ratio);
		
		strActualSpread.Format("%.2f", m_tradePair_New->ActualSpread);
		m_lblActualSpred.SetWindowTextA(strActualSpread);

		strRequiredSpread.Format("%.2f", m_tradePair_New->RequiredSpred);
		m_txtRequiredSpread.SetWindowTextA(strRequiredSpread);

		
		if( !m_tradePair_New->Comparison.Compare( ">" ) )	m_CompareSymbol = 0;
		if( !m_tradePair_New->Comparison.Compare( ">=" ) )	m_CompareSymbol = 1;
		if( !m_tradePair_New->Comparison.Compare( "<" ) )	m_CompareSymbol = 2;
		if( !m_tradePair_New->Comparison.Compare( "<=" ) )	m_CompareSymbol = 3;
		if( !m_tradePair_New->Comparison.Compare( "NA" ) )	m_CompareSymbol = 4;

		if( m_tradePair_New->Nature == 'B' )	m_orderTradeType = 0;
		if( m_tradePair_New->Nature == 'S' )	m_orderTradeType = 1;

		UpdateData(false);

		switch(m_tradePair_New->Nature)
		{
		case 'B':
			m_editSymbol1.SetWindowTextA(m_tradePair_New->BuyTrade->Stock.Name);
			m_editSymbol2.SetWindowTextA(m_tradePair_New->SellTrade->Stock.Name);
			SetSymbol1(m_tradePair_New->BuyTrade->Stock.Name.GetBuffer());
			SetSymbol2(m_tradePair_New->SellTrade->Stock.Name.GetBuffer());

			if( !m_tradePair_New->BuyTrade->PriceOn.Compare( "B" ) ) ctrl_cmbSymbol1PriceOn.SetCurSel(0);
			if( !m_tradePair_New->BuyTrade->PriceOn.Compare( "A" ) ) ctrl_cmbSymbol1PriceOn.SetCurSel(1);
			if( !m_tradePair_New->BuyTrade->PriceOn.Compare( "L" ) ) ctrl_cmbSymbol1PriceOn.SetCurSel(2);

			if( !m_tradePair_New->SellTrade->PriceOn.Compare( "B" ) ) ctrl_cmbSymbol2PriceOn.SetCurSel(0);
			if( !m_tradePair_New->SellTrade->PriceOn.Compare( "A" ) ) ctrl_cmbSymbol2PriceOn.SetCurSel(1);
			if( !m_tradePair_New->SellTrade->PriceOn.Compare( "L" ) ) ctrl_cmbSymbol2PriceOn.SetCurSel(2);

			strCashSymbol1.Format("%.2f", m_tradePair_New->BuyTrade->Cash);
			m_Symbol1Cash.SetWindowTextA(strCashSymbol1);

			strCashSymbol2.Format("%.2f", m_tradePair_New->SellTrade->Cash);
			m_Symbol2Cash.SetWindowTextA(strCashSymbol2);

			
			strSharesSymbol1.Format("%d", m_tradePair_New->BuyTrade->Qty);
			m_txtSharesForSymbol1.SetWindowTextA(strSharesSymbol1);

			strSharesSymbol2.Format("%d", m_tradePair_New->SellTrade->Qty);
			m_txtSharesForSymbol2.SetWindowTextA(strSharesSymbol2);
			
			if( !m_tradePair_New->BuyTrade->OrderPriceOn.Compare( "B" ) ) m_cboOrderPriceOnSymbol1.SetCurSel(0);
			if( !m_tradePair_New->BuyTrade->OrderPriceOn.Compare( "A" ) ) m_cboOrderPriceOnSymbol1.SetCurSel(1);
			if( !m_tradePair_New->BuyTrade->OrderPriceOn.Compare( "L" ) ) m_cboOrderPriceOnSymbol1.SetCurSel(2);
			if( !m_tradePair_New->BuyTrade->OrderPriceOn.Compare( "P" ) ) m_cboOrderPriceOnSymbol1.SetCurSel(3);
			if( !m_tradePair_New->BuyTrade->OrderPriceOn.Compare( "M" ) ) m_cboOrderPriceOnSymbol1.SetCurSel(4);

			
			if( !m_tradePair_New->SellTrade->OrderPriceOn.Compare( "B" ) ) m_cboOrderPriceOnSymbol2.SetCurSel(0);
			if( !m_tradePair_New->SellTrade->OrderPriceOn.Compare( "A" ) ) m_cboOrderPriceOnSymbol2.SetCurSel(1);
			if( !m_tradePair_New->SellTrade->OrderPriceOn.Compare( "L" ) ) m_cboOrderPriceOnSymbol2.SetCurSel(2);
			if( !m_tradePair_New->SellTrade->OrderPriceOn.Compare( "P" ) ) m_cboOrderPriceOnSymbol2.SetCurSel(3);
			if( !m_tradePair_New->SellTrade->OrderPriceOn.Compare( "M" ) ) m_cboOrderPriceOnSymbol2.SetCurSel(4);
			
			break;	

		case 'S':
			m_editSymbol1.SetWindowTextA(m_tradePair_New->SellTrade->Stock.Name);
			m_editSymbol2.SetWindowTextA(m_tradePair_New->BuyTrade->Stock.Name);
			SetSymbol1(m_tradePair_New->SellTrade->Stock.Name.GetBuffer());
			SetSymbol2(m_tradePair_New->BuyTrade->Stock.Name.GetBuffer());

			if( !m_tradePair_New->SellTrade->PriceOn.Compare( "B" ) ) ctrl_cmbSymbol1PriceOn.SetCurSel(0);
			if( !m_tradePair_New->SellTrade->PriceOn.Compare( "A" ) ) ctrl_cmbSymbol1PriceOn.SetCurSel(1);
			if( !m_tradePair_New->SellTrade->PriceOn.Compare( "L" ) ) ctrl_cmbSymbol1PriceOn.SetCurSel(2);

			if( !m_tradePair_New->BuyTrade->PriceOn.Compare( "B" ) ) ctrl_cmbSymbol2PriceOn.SetCurSel(0);
			if( !m_tradePair_New->BuyTrade->PriceOn.Compare( "A" ) ) ctrl_cmbSymbol2PriceOn.SetCurSel(1);
			if( !m_tradePair_New->BuyTrade->PriceOn.Compare( "L" ) ) ctrl_cmbSymbol2PriceOn.SetCurSel(2);

			strCashSymbol1.Format("%.2f", m_tradePair_New->SellTrade->Cash);
			m_Symbol1Cash.SetWindowTextA(strCashSymbol1);

			strCashSymbol2.Format("%.2f", m_tradePair_New->BuyTrade->Cash);
			m_Symbol2Cash.SetWindowTextA(strCashSymbol2);

			strSharesSymbol1.Format("%d", m_tradePair_New->SellTrade->Qty);
			m_txtSharesForSymbol1.SetWindowTextA(strSharesSymbol1);

			strSharesSymbol2.Format("%d", m_tradePair_New->BuyTrade->Qty);
			m_txtSharesForSymbol2.SetWindowTextA(strSharesSymbol2);

			if( !m_tradePair_New->SellTrade->OrderPriceOn.Compare( "B" ) ) m_cboOrderPriceOnSymbol1.SetCurSel(0);
			if( !m_tradePair_New->SellTrade->OrderPriceOn.Compare( "A" ) ) m_cboOrderPriceOnSymbol1.SetCurSel(1);
			if( !m_tradePair_New->SellTrade->OrderPriceOn.Compare( "L" ) ) m_cboOrderPriceOnSymbol1.SetCurSel(2);
			if( !m_tradePair_New->SellTrade->OrderPriceOn.Compare( "P" ) ) m_cboOrderPriceOnSymbol1.SetCurSel(3);
			if( !m_tradePair_New->SellTrade->OrderPriceOn.Compare( "M" ) ) m_cboOrderPriceOnSymbol1.SetCurSel(4);
			
			if( !m_tradePair_New->BuyTrade->OrderPriceOn.Compare( "B" ) ) m_cboOrderPriceOnSymbol2.SetCurSel(0);
			if( !m_tradePair_New->BuyTrade->OrderPriceOn.Compare( "A" ) ) m_cboOrderPriceOnSymbol2.SetCurSel(1);
			if( !m_tradePair_New->BuyTrade->OrderPriceOn.Compare( "L" ) ) m_cboOrderPriceOnSymbol2.SetCurSel(2);
			if( !m_tradePair_New->BuyTrade->OrderPriceOn.Compare( "P" ) ) m_cboOrderPriceOnSymbol2.SetCurSel(3);
			if( !m_tradePair_New->BuyTrade->OrderPriceOn.Compare( "M" ) ) m_cboOrderPriceOnSymbol2.SetCurSel(4);

			break;
		}
		UpdateMarketComboBox();
	}
	
	L_AttachToAppNotifier(this);

	return TRUE;
}

BEGIN_MESSAGE_MAP(PairCalculatorDetail, CDialog)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDOK, &PairCalculatorDetail::OnBnClickedOk)
	ON_EN_KILLFOCUS(IDC_SYMBOL1, &PairCalculatorDetail::OnLostFocusSymbol1)
	ON_EN_KILLFOCUS(IDC_SYMBOL2, &PairCalculatorDetail::OnLostFocusSymbol2)
	ON_EN_CHANGE(IDC_txtRatio, &PairCalculatorDetail::OnTxtRatioChanged)
	ON_EN_CHANGE(TxtSharesForSymbol1, &PairCalculatorDetail::OnTxtQtyChangeSymbol1)
	ON_CBN_SELCHANGE(CmbTradeAtSymbol1, &PairCalculatorDetail::OnOrderPriceOnChangedSymbol1)
	ON_CBN_SELCHANGE(CmbTradeAtSymbol2, &PairCalculatorDetail::OnOrderPriceOnChangedSymbol2)
	ON_CBN_SELCHANGE(IDC_cmbSym1Price, &PairCalculatorDetail::OnPriceOnChangedSymbol1)
	ON_CBN_SELCHANGE(IDC_cmbSym2Price, &PairCalculatorDetail::OnPriceOnChangedSymbol2)
	ON_BN_CLICKED(IDC_GT, &PairCalculatorDetail::OnCompareGTClicked)
	ON_BN_CLICKED(IDC_GTET, &PairCalculatorDetail::OnCompareGTEClicked)
	ON_BN_CLICKED(IDC_LT, &PairCalculatorDetail::OnCompareLTClicked)
	ON_BN_CLICKED(IDC_LTET, &PairCalculatorDetail::OnCompareLTEClicked)
	ON_BN_CLICKED(rbTradeTypeBuy, &PairCalculatorDetail::OnTradeTypeBuyClicked)
	ON_BN_CLICKED(rbTradeTypeSell, &PairCalculatorDetail::OnTradeTypeSellClicked)
	ON_EN_CHANGE(IDC_SYMBOLEDIT4, &PairCalculatorDetail::OnTxtRequiredSpreadChanged)
	ON_BN_CLICKED(IDCANCEL, &PairCalculatorDetail::OnBnClickedCancel)
	ON_CBN_SELCHANGE(CmbTradeOnSymbol1, &PairCalculatorDetail::OnMarketChangedSymbol1)
	ON_CBN_SELCHANGE(CmbTradeOnSymbol2, &PairCalculatorDetail::OnMarketChangedSymbol2)
END_MESSAGE_MAP()

//L_Observer
void PairCalculatorDetail::HandleMessage(L_Message const *pMsg)
{
	long returnType = pMsg->L_Type();
	switch (returnType)
	{
	case L_MsgSymbolChanged::id:
		{
			/*if (m_Linked)
			{
				SetSymbol(pMsg->L_Symbol());
			}*/
		}
		break;
	case L_MsgL1::id:
	case L_MsgL1Change::id:
		{
			/*if (fullQuote && !strcmp(pMsg->L_Symbol(),fullQuote->L_Symbol()))
			{
				UpdateStaticFQData();
				UpdateL1FQData();
			}*/
			UpdateMarketComboBox();
		}
		break;
	case L_MsgL1Update::id:
		{
			/*if (fullQuote && !strcmp(pMsg->L_Symbol(), fullQuote->L_Symbol()))
			{
				UpdateL1FQData();
			}*/
			GetLivePrice();
		}
		break;
	case L_MsgL2::id:
		UpdateMarketComboBox();
	case L_MsgL2Refresh::id:
	case L_MsgL2Update::id:
	case L_MsgECN::id:
	case L_MsgECNUpdate::id:
		{
			/*if (fullQuote && !strcmp(pMsg->L_Symbol(), fullQuote->L_Symbol()))
			{
				UpdateL1FQData();
				UpdateL2FQData();
				UpdateBestQuotes();
				UpdateBook();
			}*/
		}
		break;
	case L_MsgTrade::id:
		/*{
			InsertInitialTrades();
		}*/
		break;
	case L_MsgTradeUpdate::id:
		{
			/*if (fullQuote && !strcmp(fullQuote->L_Symbol(), pMsg->L_Symbol()))
			{
				L_MsgTradeUpdate const *pMsgTU = static_cast<L_MsgTradeUpdate const *>(pMsg);
				InsertNewTrade
					( pMsgTU->L_Price()
					, pMsgTU->L_Volume()
					, pMsgTU->L_Market()
					, pMsgTU->L_SaleCondition()
					, pMsgTU->L_Time()
					);

				m_TradesListCtrl.Invalidate(FALSE);
			}*/
		}
		break;
	case L_MsgECNList::id:
		{
			UpdateMarketComboBox();
		}
		break;
	}
}
void PairCalculatorDetail::OnClose()
{
	// TODO: Add your message handler code here and/or call default
	isFormClosing = true;
	__super::OnClose();
}

void PairCalculatorDetail::SetSymbol1(const char *symbol)
{
	if (fullQuote1)
	{
		fullQuote1->L_Detach(this);
		L_DestroyFullQuote(fullQuote1);
	}

	fullQuote1 = L_CreateFullQuote(symbol);
	fullQuote1->L_Attach(this);
	
	if(IsNewTrade)
	{
		if(m_tradePair_New->Nature == 'B')
		{
			//Trade buyTrade;
			m_tradePair_New->BuyTrade->Stock.Name = symbol;
			if (m_tradePair_New->BuyTrade->Summary)
			{
				m_tradePair_New->BuyTrade->Summary->L_Detach(this);
				L_DestroySummary(m_tradePair_New->BuyTrade->Summary);
			}

			m_tradePair_New->BuyTrade->Summary = L_CreateSummary(m_tradePair_New->BuyTrade->Stock.Name);
			m_tradePair_New->BuyTrade->Summary->L_Attach(this);
		}
		else
		{
			//Trade buyTrade;
			m_tradePair_New->SellTrade->Stock.Name = symbol;
			if (m_tradePair_New->SellTrade->Summary)
			{
				m_tradePair_New->SellTrade->Summary->L_Detach(this);
				L_DestroySummary(m_tradePair_New->SellTrade->Summary);
			}

			m_tradePair_New->SellTrade->Summary = L_CreateSummary(m_tradePair_New->SellTrade->Stock.Name);
			m_tradePair_New->SellTrade->Summary->L_Attach(this);
		}
	}
	
	UpdateMarketComboBox();
}

void PairCalculatorDetail::SetSymbol2(const char *symbol)
{
	if (fullQuote2)
	{
		fullQuote2->L_Detach(this);
		L_DestroyFullQuote(fullQuote2);
	}

	fullQuote2 = L_CreateFullQuote(symbol);
	fullQuote2->L_Attach(this);

	if(IsNewTrade)
	{
		if(m_tradePair_New->Nature == 'B')
		{
			//Trade sellTrade;
			m_tradePair_New->SellTrade->Stock.Name = symbol;
			if(m_tradePair_New->Ratio > 0)
			{
				m_tradePair_New->SellTrade->Qty = (int)(m_tradePair_New->BuyTrade->Qty / m_tradePair_New->Ratio);
			}
			if (m_tradePair_New->SellTrade->Summary)
			{
				m_tradePair_New->SellTrade->Summary->L_Detach(this);
				L_DestroySummary(m_tradePair_New->SellTrade->Summary);
			}

			m_tradePair_New->SellTrade->Summary = L_CreateSummary(m_tradePair_New->SellTrade->Stock.Name);
			m_tradePair_New->SellTrade->Summary->L_Attach(this);
		}
		else
		{
			//Trade sellTrade;
			m_tradePair_New->BuyTrade->Stock.Name = symbol;
			if(m_tradePair_New->Ratio > 0)
			{
				m_tradePair_New->BuyTrade->Qty = (int)(m_tradePair_New->SellTrade->Qty / m_tradePair_New->Ratio);
			}
			if (m_tradePair_New->BuyTrade->Summary)
			{
				m_tradePair_New->BuyTrade->Summary->L_Detach(this);
				L_DestroySummary(m_tradePair_New->BuyTrade->Summary);
			}

			m_tradePair_New->BuyTrade->Summary = L_CreateSummary(m_tradePair_New->BuyTrade->Stock.Name);
			m_tradePair_New->BuyTrade->Summary->L_Attach(this);
		}
	}
	UpdateMarketComboBox();
}

void PairCalculatorDetail::UpdateMarketComboBox()
{
	if(!CanCalculate()) return;
	if(isFormClosing) return;
	

	if (fullQuote1)
	{
		//CString curString;
		

		/*while (m_DestinationComboSymbol1.GetCount() > 0)
		{
			m_DestinationComboSymbol1.DeleteString(0);
		}

		for (direct_book_iterator it(fullQuote1->ecn_names_begin()), itend(fullQuote1->ecn_names_end());
			it != itend; ++it)
		{
			
			m_DestinationComboSymbol1.AddString(*it);
		}*/
		
	}

	if (fullQuote2)
	{
		/*CString curString;
		while (m_DestinationComboSymbol2.GetCount() > 0)
		{
			m_DestinationComboSymbol2.DeleteString(0);
		}

		for (direct_book_iterator it(fullQuote2->ecn_names_begin()), itend(fullQuote2->ecn_names_end());
			it != itend; ++it)
		{
			m_DestinationComboSymbol2.AddString(*it);
		}*/
		
		//m_DestinationComboSymbol2.SetWindowText(curString);
	}
	GetLivePrice();
	UpdateControls();
	UpdateData(false);
}

void PairCalculatorDetail::OnBnClickedOk()
{
	CString validationError =  ValidatePairTrade();
	if(validationError.IsEmpty())//If no error, go ahead
	{
		m_tradePair_Old = m_tradePair_New;
		isFormClosing = true;
		this->EndDialog(IDOK);
	}
}

void PairCalculatorDetail::FillAll()
{
	
	m_tradePair_New->AssignPairTradeID();

	switch(m_orderTradeType)
	{
	case 0:// Buy
		m_tradePair_New->Nature = 'B';
		break;
	case 1:// Sell
		m_tradePair_New->Nature = 'S';
		break;
	}
	
	CString ratio;
	ctrl_txtRatio.GetWindowText(ratio);
	if( ratio.IsEmpty() )
	{
		m_tradePair_New->Ratio = 0;
	}
	else
	{
		m_tradePair_New->Ratio = atof(ratio);
	}
	
	SetStockValues(m_tradePair_New);

	switch(m_CompareSymbol)
	{
	case 0:// (>) Greater Than
		m_tradePair_New->Comparison = ">";
		break;
	case 1:// (>=) Greater Than or Equal To
		m_tradePair_New->Comparison = ">=";
		break;
	case 2:// (<) Less Than
		m_tradePair_New->Comparison = "<";
		break;
	case 3:// (<=) Less Than or Equal To
		m_tradePair_New->Comparison = "<=";
		break;
	default:
		m_tradePair_New->Comparison = "NA";
		break;
	}
	
	CString reuiredSpread = "";
	m_txtRequiredSpread.GetWindowText(reuiredSpread);
	if( reuiredSpread.IsEmpty() )
	{
		m_tradePair_New->RequiredSpred = 0;
	}
	else
	{
		m_tradePair_New->RequiredSpred = atof(reuiredSpread);
	}
}

void PairCalculatorDetail::SetStockValues(PairTrade *tradePair)
{
	if(tradePair->Nature == 'B')//Set Symbol 1 to BuyStock if it is Buy Trade
	{
		SetBuyStockAsSymbol1(tradePair);
		SetSellStockAsSymbol2(tradePair);
	}
	else//Set Symbol 2 to BuyStock if it is Sell Trade
	{
		SetSellStockAsSymbol1(tradePair);
		SetBuyStockAsSymbol2(tradePair);
	}
}

void PairCalculatorDetail::SetBuyStockAsSymbol1(PairTrade *tradePair)
{
	CString symbol1 = "";
	m_editSymbol1.GetWindowTextA(symbol1);
	
	if (tradePair->BuyTrade->Stock.Name != symbol1.MakeUpper())
	{
		SetSymbol1(symbol1.MakeUpper());
		tradePair->BuyTrade->Stock.Name = symbol1.MakeUpper();
	}
	{//Set PriceOn - Bid/Ask/Last for symbol1
		int iIndex = 0;
		CString symbol1PriceOn;
		iIndex = ctrl_cmbSymbol1PriceOn.GetCurSel();
		if(iIndex >= 0)
		{
 			ctrl_cmbSymbol1PriceOn.GetLBText(iIndex, symbol1PriceOn); 
		}
		if(!symbol1PriceOn.IsEmpty())
		{
			if( !symbol1PriceOn.Compare( "Bid" ) )
			{
				tradePair->BuyTrade->PriceOn = "B";
			}
			else if( !symbol1PriceOn.Compare( "Ask" ) )
			{
				tradePair->BuyTrade->PriceOn = "A";
			}
			else if( !symbol1PriceOn.Compare( "Last" ) )
			{
				tradePair->BuyTrade->PriceOn = "L";
			}
		}
	}
		
	{// Set Cash for Symbol1
		CString strSymbol1Cash = "";
		m_Symbol1Cash.GetWindowTextA(strSymbol1Cash);
		if(!strSymbol1Cash.IsEmpty())
		{
			tradePair->BuyTrade->Cash = atof(strSymbol1Cash);
		}
		else
		{
			tradePair->BuyTrade->Cash = 0;
		}
	}

	{//Set No of Shares for Symbol 1
		CString sharesSymbol1;
		m_txtSharesForSymbol1.GetWindowText(sharesSymbol1);
		if( !sharesSymbol1.IsEmpty() )
		{
			tradePair->BuyTrade->Qty = atoi(sharesSymbol1);
		}
		else
		{
			tradePair->BuyTrade->Qty = 0;
		}
	}

	{//Set Order PriceOn - Bid/Ask/Last/Mid Point/Market for symbol1
		int iIndex = 0;
		CString symbol1OrderPriceOn;
		iIndex = m_cboOrderPriceOnSymbol1.GetCurSel();
		if(iIndex >= 0)
		{
 			m_cboOrderPriceOnSymbol1.GetLBText(iIndex, symbol1OrderPriceOn); 
		}
		
		if(!symbol1OrderPriceOn.IsEmpty())
		{
			if( !symbol1OrderPriceOn.Compare( "Bid" ) )
			{
				tradePair->BuyTrade->OrderPriceOn = "B";
			}
			else if( !symbol1OrderPriceOn.Compare( "Ask" ) )
			{
				tradePair->BuyTrade->OrderPriceOn = "A";
			}
			else if( !symbol1OrderPriceOn.Compare( "Last" ) )
			{
				tradePair->BuyTrade->OrderPriceOn = "L";
			}
			else if( !symbol1OrderPriceOn.Compare( "Mid Pont" ) )
			{
				tradePair->BuyTrade->OrderPriceOn = "P";
			}
			else if( !symbol1OrderPriceOn.Compare( "Market" ) )
			{
				tradePair->BuyTrade->OrderPriceOn = "M";
			}
		}
	}
	{//Set Order Destination for symbol 1
		int iIndex = 0;
		CString orderDestination1;
		iIndex = m_DestinationComboSymbol1.GetCurSel();
		if(iIndex >= 0)
		{
 			m_DestinationComboSymbol1.GetLBText(iIndex, orderDestination1); 
		}
		tradePair->BuyTrade->OrderDestination = "";
		if(!orderDestination1.IsEmpty())
		{
			tradePair->BuyTrade->OrderDestination = orderDestination1;
		}
	}
}
void PairCalculatorDetail::SetBuyStockAsSymbol2(PairTrade *tradePair)
{
	CString symbol2 = "";
	m_editSymbol2.GetWindowTextA(symbol2);
	
	if (tradePair->BuyTrade->Stock.Name != symbol2.MakeUpper())
	{
		SetSymbol2(symbol2.MakeUpper());
		tradePair->BuyTrade->Stock.Name = symbol2.MakeUpper();
	}
	{//Set PriceOn - Bid/Ask/Last for symbol2
		int iIndex = 0;
		CString symbol2PriceOn;
		iIndex = ctrl_cmbSymbol2PriceOn.GetCurSel();
		if(iIndex >= 0)
		{
 			ctrl_cmbSymbol2PriceOn.GetLBText(iIndex, symbol2PriceOn); 
		}
		tradePair->BuyTrade->PriceOn = "";
		if(!symbol2PriceOn.IsEmpty())
		{
			if( !symbol2PriceOn.Compare( "Bid" ) )
			{
				tradePair->BuyTrade->PriceOn = "B";
			}
			else if( !symbol2PriceOn.Compare( "Ask" ) )
			{
				tradePair->BuyTrade->PriceOn = "A";
			}
			else if( !symbol2PriceOn.Compare( "Last" ) )
			{
				tradePair->BuyTrade->PriceOn = "L";
			}
		}
	}
		

	{// Set Cash for Symbol2
		CString strSymbol2Cash = "";
		m_Symbol2Cash.GetWindowTextA(strSymbol2Cash);
		if(!strSymbol2Cash.IsEmpty())
		{
			tradePair->BuyTrade->Cash = atof(strSymbol2Cash);
		}
		else
		{
			tradePair->BuyTrade->Cash = 0;
		}
	}
	{//Set No of Shares for Symbol 2
		CString sharesSymbol2;
		m_txtSharesForSymbol2.GetWindowText(sharesSymbol2);
		if( !sharesSymbol2.IsEmpty() )
		{
			tradePair->BuyTrade->Qty = atoi(sharesSymbol2);
		}
		else
		{
			tradePair->BuyTrade->Qty = 0;
		}
	}
	{//Set Order PriceOn - Bid/Ask/Last/Mid Point/Market for symbol2
		int iIndex = 0;
		CString symbol2OrderPriceOn;
		iIndex = m_cboOrderPriceOnSymbol2.GetCurSel();
		if(iIndex >= 0)
		{
 			m_cboOrderPriceOnSymbol2.GetLBText(iIndex, symbol2OrderPriceOn); 
		}
		
		if(!symbol2OrderPriceOn.IsEmpty())
		{
			if( !symbol2OrderPriceOn.Compare( "Bid" ) )
			{
				tradePair->BuyTrade->OrderPriceOn = "B";
			}
			else if( !symbol2OrderPriceOn.Compare( "Ask" ) )
			{
				tradePair->BuyTrade->OrderPriceOn = "A";
			}
			else if( !symbol2OrderPriceOn.Compare( "Last" ) )
			{
				tradePair->BuyTrade->OrderPriceOn = "L";
			}
			else if( !symbol2OrderPriceOn.Compare( "Mid Pont" ) )
			{
				tradePair->BuyTrade->OrderPriceOn = "P";
			}
			else if( !symbol2OrderPriceOn.Compare( "Market" ) )
			{
				tradePair->BuyTrade->OrderPriceOn = "M";
			}
		}
	}

	{//Set Order Destination for symbol 2
		int iIndex = 0;
		CString orderDestination2;
		iIndex = m_DestinationComboSymbol2.GetCurSel();
		if(iIndex >= 0)
		{
 			m_DestinationComboSymbol2.GetLBText(iIndex, orderDestination2); 
		}
		tradePair->BuyTrade->OrderDestination = "";
		if(!orderDestination2.IsEmpty())
		{
			tradePair->BuyTrade->OrderDestination = orderDestination2;
		}
	}
}
void PairCalculatorDetail::SetSellStockAsSymbol2(PairTrade *tradePair)
{
	CString symbol2 = "";
	m_editSymbol2.GetWindowTextA(symbol2);
	
	if (tradePair->SellTrade->Stock.Name != symbol2.MakeUpper())
	{
		SetSymbol2(symbol2.MakeUpper());
		tradePair->SellTrade->Stock.Name = symbol2.MakeUpper();
	}
	{//Set PriceOn - Bid/Ask/Last for symbol2
		int iIndex = 0;
		CString symbol2PriceOn;
		iIndex = ctrl_cmbSymbol2PriceOn.GetCurSel();
		if(iIndex >= 0)
		{
 			ctrl_cmbSymbol2PriceOn.GetLBText(iIndex, symbol2PriceOn); 
		}
		tradePair->SellTrade->PriceOn = "";
		if(!symbol2PriceOn.IsEmpty())
		{
			if( !symbol2PriceOn.Compare( "Bid" ) )
			{
				tradePair->SellTrade->PriceOn = "B";
			}
			else if( !symbol2PriceOn.Compare( "Ask" ) )
			{
				tradePair->SellTrade->PriceOn = "A";
			}
			else if( !symbol2PriceOn.Compare( "Last" ) )
			{
				tradePair->SellTrade->PriceOn = "L";
			}
		}
	}
				
	

	{// Set Cash for Symbol2
		CString strSymbol2Cash = "";
		m_Symbol2Cash.GetWindowTextA(strSymbol2Cash);
		if(!strSymbol2Cash.IsEmpty())
		{
			tradePair->SellTrade->Cash = atof(strSymbol2Cash);
		}
		else
		{
			tradePair->SellTrade->Cash = 0;
		}
	}

	{//Set No of Shares for Symbol 2
		CString sharesSymbol2;
		m_txtSharesForSymbol2.GetWindowText(sharesSymbol2);
		if( !sharesSymbol2.IsEmpty() )
		{
			tradePair->SellTrade->Qty = atoi(sharesSymbol2);
		}
		else
		{
			tradePair->SellTrade->Qty = 0;
		}
	}
	{//Set Order PriceOn - Bid/Ask/Last/Mid Point/Market for symbol2
		int iIndex = 0;
		CString symbol2OrderPriceOn;
		iIndex = m_cboOrderPriceOnSymbol2.GetCurSel();
		if(iIndex >= 0)
		{
 			m_cboOrderPriceOnSymbol2.GetLBText(iIndex, symbol2OrderPriceOn); 
		}
		
		if(!symbol2OrderPriceOn.IsEmpty())
		{
			if( !symbol2OrderPriceOn.Compare( "Bid" ) )
			{
				tradePair->SellTrade->OrderPriceOn = "B";
			}
			else if( !symbol2OrderPriceOn.Compare( "Ask" ) )
			{
				tradePair->SellTrade->OrderPriceOn = "A";
			}
			else if( !symbol2OrderPriceOn.Compare( "Last" ) )
			{
				tradePair->SellTrade->OrderPriceOn = "L";
			}
			else if( !symbol2OrderPriceOn.Compare( "Mid Pont" ) )
			{
				tradePair->SellTrade->OrderPriceOn = "P";
			}
			else if( !symbol2OrderPriceOn.Compare( "Market" ) )
			{
				tradePair->SellTrade->OrderPriceOn = "M";
			}
		}
	}
	{//Set Order Destination for symbol 2
		int iIndex = 0;
		CString orderDestination2;
		iIndex = m_DestinationComboSymbol2.GetCurSel();
		if(iIndex >= 0)
		{
 			m_DestinationComboSymbol2.GetLBText(iIndex, orderDestination2); 
		}
		tradePair->SellTrade->OrderDestination = "";
		if(!orderDestination2.IsEmpty())
		{
			tradePair->SellTrade->OrderDestination = orderDestination2;
		}
	}
}
void PairCalculatorDetail::SetSellStockAsSymbol1(PairTrade *tradePair)
{
	CString symbol1 = "";
	m_editSymbol1.GetWindowTextA(symbol1);

	if (tradePair->SellTrade->Stock.Name != symbol1.MakeUpper())
	{
		SetSymbol1(symbol1.MakeUpper());
		tradePair->SellTrade->Stock.Name = symbol1.MakeUpper();
	}
	{//Set PriceOn - Bid/Ask/Last for symbol1
		int iIndex = 0;
		CString symbol1PriceOn;
		iIndex = ctrl_cmbSymbol1PriceOn.GetCurSel();
		if(iIndex >= 0)
		{
 			ctrl_cmbSymbol1PriceOn.GetLBText(iIndex, symbol1PriceOn); 
		}
		
		if(!symbol1PriceOn.IsEmpty())
		{
			if( !symbol1PriceOn.Compare( "Bid" ) )
			{
				tradePair->SellTrade->PriceOn = "B";
			}
			else if( !symbol1PriceOn.Compare( "Ask" ) )
			{
				tradePair->SellTrade->PriceOn = "A";
			}
			else if( !symbol1PriceOn.Compare( "Last" ) )
			{
				tradePair->SellTrade->PriceOn = "L";
			}
		}
	}
				
	

	{// Set Cash for Symbol1
		CString strSymbol1Cash = "";
		m_Symbol1Cash.GetWindowTextA(strSymbol1Cash);
		if(!strSymbol1Cash.IsEmpty())
		{
			tradePair->SellTrade->Cash = atof(strSymbol1Cash);
		}
		else
		{
			tradePair->SellTrade->Cash = 0;
		}
	}

	{//Set No of Shares for Symbol 1
		CString sharesSymbol1;
		m_txtSharesForSymbol1.GetWindowText(sharesSymbol1);
		if( !sharesSymbol1.IsEmpty() )
		{
			tradePair->SellTrade->Qty = atoi(sharesSymbol1);
		}
		else
		{
			tradePair->SellTrade->Qty = 0;
		}
	}
	{//Set Order PriceOn - Bid/Ask/Last/Mid Point/Market for symbol1
		int iIndex = 0;
		CString symbol1OrderPriceOn;
		iIndex = m_cboOrderPriceOnSymbol1.GetCurSel();
		if(iIndex >= 0)
		{
 			m_cboOrderPriceOnSymbol1.GetLBText(iIndex, symbol1OrderPriceOn); 
		}
		
		if(!symbol1OrderPriceOn.IsEmpty())
		{
			if( !symbol1OrderPriceOn.Compare( "Bid" ) )
			{
				tradePair->SellTrade->OrderPriceOn = "B";
			}
			else if( !symbol1OrderPriceOn.Compare( "Ask" ) )
			{
				tradePair->SellTrade->OrderPriceOn = "A";
			}
			else if( !symbol1OrderPriceOn.Compare( "Last" ) )
			{
				tradePair->SellTrade->OrderPriceOn = "L";
			}
			else if( !symbol1OrderPriceOn.Compare( "Mid Pont" ) )
			{
				tradePair->SellTrade->OrderPriceOn = "P";
			}
			else if( !symbol1OrderPriceOn.Compare( "Market" ) )
			{
				tradePair->SellTrade->OrderPriceOn = "M";
			}
		}
	}
	{//Set Order Destination for symbol 1
		int iIndex = 0;
		CString orderDestination1;
		iIndex = m_DestinationComboSymbol1.GetCurSel();
		if(iIndex >= 0)
		{
 			m_DestinationComboSymbol1.GetLBText(iIndex, orderDestination1);
		}
		tradePair->SellTrade->OrderDestination = "";
		if(!orderDestination1.IsEmpty())
		{
			tradePair->SellTrade->OrderDestination = orderDestination1;
		}
	}
}
CString PairCalculatorDetail::ValidatePairTrade()
{
	///Validate all values
	CString returnMessage = "";
	CString symbol1 = "";
	CString symbol2 = "";
	{//Validate Symbols
		{//Validate Symbol1
			m_editSymbol1.GetWindowTextA(symbol1);
			if(symbol1.IsEmpty())
			{
				returnMessage = "Please select symbol 1";
				MessageBox(returnMessage, "Pair Trade");
				return returnMessage;
			}
		}
		{//Validate Symbol2
			m_editSymbol2.GetWindowTextA(symbol2);
			if(symbol2.IsEmpty())
			{
				returnMessage = "Please select symbol 2";
				MessageBox(returnMessage, "Pair Trade");
				return returnMessage;
			}
		}
	}
	{//Validate Compare Symbol
		switch(m_CompareSymbol)
		{
		case 0:// (>) Greater Than
			break;
		case 1:// (>=) Greater Than or Equal To
			break;
		case 2:// (<) Less Than
			break;
		case 3:// (<=) Less Than or Equal To
			break;
		default:
			returnMessage = "Please select the Comparision operator";
			break;
		}
		if(!returnMessage.IsEmpty())
		{
			MessageBox(returnMessage, "Pair Trade");
			return returnMessage;
		}
	}

	/////Order Setting Validations////////////////////////////////////////////
	{//Validate Trade Type
		switch(m_orderTradeType)
		{
		case 0:// Buy
			break;
		case 1:// Sell
			break;
		}
		if(!returnMessage.IsEmpty())
		{
			MessageBox(returnMessage, "Pair Trade");
			return returnMessage;
		}
	}

	{//Validate No of Shares for Symbol 1
		CString sharesSymbol1;
		int noOfSharesSymbol1 = 0;
		m_txtSharesForSymbol1.GetWindowText(sharesSymbol1);
		if( sharesSymbol1.IsEmpty() )
		{
			noOfSharesSymbol1 = 0;
			returnMessage = "Please enter number of shares for " + symbol1;
		}
		else
		{
			noOfSharesSymbol1 = atoi(sharesSymbol1);
		}
	}

	{//Validate No of Shares for Symbol 2
		CString sharesSymbol2;
		int noOfSharesSymbol2 = 0;
		m_txtSharesForSymbol2.GetWindowText(sharesSymbol2);
		if( sharesSymbol2.IsEmpty() )
		{
			noOfSharesSymbol2 = 0;
			returnMessage = "Please enter number of shares for " + symbol2;
		}
		else
		{
			noOfSharesSymbol2 = atoi(sharesSymbol2);
		}
	}

	{//Validate Order Destinations
		
		{//Validate Order Destination for symbol 1
			int iIndex = 0;
			CString orderDestination1;
			iIndex = m_DestinationComboSymbol1.GetCurSel();
 			m_DestinationComboSymbol1.GetLBText(iIndex, orderDestination1); 
			if(orderDestination1.IsEmpty())
			{
				returnMessage = "Please select the Destination for " + symbol1;
				MessageBox(returnMessage, "Pair Trade");
				return returnMessage;
			}
		}

		{//Validate Order Destination for symbol 2
			int iIndex = 0;
			CString orderDestination2;
			iIndex = m_DestinationComboSymbol2.GetCurSel();
 			m_DestinationComboSymbol1.GetLBText(iIndex, orderDestination2); 
			if(orderDestination2.IsEmpty())
			{
				returnMessage = "Please select the Destination for " + symbol2;
				MessageBox(returnMessage, "Pair Trade");
				return returnMessage;
			}
		}
	}
	return returnMessage;
}

void PairCalculatorDetail::OnLostFocusSymbol1()
{
	CString symbol;
	m_editSymbol1.GetWindowText(symbol);
	symbol.Trim().MakeUpper();
	if (! symbol.IsEmpty())
	{
		if (!fullQuote1 || strcmp(symbol.GetBuffer(), fullQuote1->L_Symbol()) != 0)
		{
			SetSymbol1(symbol.GetBuffer());
		}
	}
}

void PairCalculatorDetail::OnLostFocusSymbol2()
{
	CString symbol;
	m_editSymbol2.GetWindowText(symbol);
	symbol.Trim().MakeUpper();
	if (! symbol.IsEmpty())
	{
		if (!fullQuote2 || strcmp(symbol.GetBuffer(), fullQuote2->L_Symbol()) != 0)
		{
			SetSymbol2(symbol.GetBuffer());
		}
	}
}

void PairCalculatorDetail::GetLivePrice()
{
	if(!CanCalculate()) return;

	if(!isFormClosing)
	{
		if(m_tradePair_New)
		{
			if(m_tradePair_New->BuyTrade->Summary)
			{
				  //m_tradePair_New->BuyTrade->Price = m_tradePair_New->BuyTrade->Summary->L_Bid();
				if(m_tradePair_New->BuyTrade->PriceOn == "B")
				{
					m_tradePair_New->BuyTrade->Price = m_tradePair_New->BuyTrade->Summary->L_Bid();
				}
				else if(m_tradePair_New->BuyTrade->PriceOn == "A")
				{
					m_tradePair_New->BuyTrade->Price = m_tradePair_New->BuyTrade->Summary->L_Ask();
				}
				else if(m_tradePair_New->BuyTrade->PriceOn == "L")
				{
					m_tradePair_New->BuyTrade->Price = m_tradePair_New->BuyTrade->Summary->L_LastPrice();
				}
			}
			if(m_tradePair_New->SellTrade->Summary)
			{
				//m_tradePair_New->SellTrade->Price = m_tradePair_New->SellTrade->Summary->L_Ask();
				if(m_tradePair_New->SellTrade->PriceOn == "B")
				{
					m_tradePair_New->SellTrade->Price = m_tradePair_New->SellTrade->Summary->L_Bid();
				}
				else if(m_tradePair_New->SellTrade->PriceOn == "A")
				{
					m_tradePair_New->SellTrade->Price = m_tradePair_New->SellTrade->Summary->L_Ask();
				}
				else if(m_tradePair_New->SellTrade->PriceOn == "L")
				{
					m_tradePair_New->SellTrade->Price = m_tradePair_New->SellTrade->Summary->L_LastPrice();
				}
			}
		}
	}
	UpdateDynamicValues();
	UpdateControls();
}

void PairCalculatorDetail::UpdateControls()
{
	if(!CanCalculate()) return;
	if(isFormClosing) return;
	if(m_tradePair_New)
	{
		

		CString text;
		text.Format("%.2f", m_tradePair_New->ActualSpread);
		m_lblActualSpred.SetWindowTextA(text);
		m_lblEvalution.SetWindowTextA((m_tradePair_New->Evalution ? "TRUE" : "FALSE"));

		CString curString;
		if(m_tradePair_New->Nature == 'B')
		{
			m_lblSharesForSymbol1.SetWindowTextA("Shares for " + m_tradePair_New->BuyTrade->Stock.Name);
			m_lblSharesForSymbol2.SetWindowTextA("Shares for " + m_tradePair_New->SellTrade->Stock.Name);
			curString = m_tradePair_New->BuyTrade->OrderDestination;
		}
		else
		{
			m_lblSharesForSymbol2.SetWindowTextA("Shares for " + m_tradePair_New->BuyTrade->Stock.Name);
			m_lblSharesForSymbol1.SetWindowTextA("Shares for " + m_tradePair_New->SellTrade->Stock.Name);
			curString = m_tradePair_New->SellTrade->OrderDestination;
		}
		int iIndex = -1;
		iIndex = m_DestinationComboSymbol1.GetCurSel();
		if(iIndex >= 0)
		{
 			m_DestinationComboSymbol1.GetLBText(iIndex, curString); 
		}
		if(curString.IsEmpty())
		{
			if(m_DestinationComboSymbol1.GetCount() > 0)
			{
				m_DestinationComboSymbol1.SetCurSel(0);
			}
		}
		else
		{
			CString tempString;
			for(int i=0; i< m_DestinationComboSymbol1.GetCount(); i++)
			{
				m_DestinationComboSymbol1.GetLBText(i, tempString); 
				if(tempString == curString)
				{
					m_DestinationComboSymbol1.SetCurSel(i);	
				}
			}
		}

		if(m_tradePair_New->Nature == 'B')
		{
			curString = m_tradePair_New->SellTrade->OrderDestination;
		}
		else
		{
			curString = m_tradePair_New->BuyTrade->OrderDestination;
		}
		iIndex = -1;
		iIndex = m_DestinationComboSymbol2.GetCurSel();
		if(iIndex >= 0)
		{
 			m_DestinationComboSymbol2.GetLBText(iIndex, curString); 
		}
		if(curString.IsEmpty())
		{
			if(m_DestinationComboSymbol2.GetCount() > 0)
			{
				m_DestinationComboSymbol2.SetCurSel(0);
			}
		}
		else
		{
			CString tempString;
			for(int i=0; i< m_DestinationComboSymbol2.GetCount(); i++)
			{
				m_DestinationComboSymbol2.GetLBText(i, tempString); 
				if(tempString == curString)
				{
					m_DestinationComboSymbol2.SetCurSel(i);	
				}
			}
			//m_DestinationComboSymbol2.SetWindowText(curString);
		}

		curString.Empty();
		if(m_tradePair_New->Nature == 'B')
		{
			curString.Format("%d",m_tradePair_New->SellTrade->Qty);
			m_txtSharesForSymbol2.SetWindowTextA(curString);
		}
		else
		{
			curString.Format("%d",m_tradePair_New->BuyTrade->Qty);
			m_txtSharesForSymbol2.SetWindowTextA(curString);
		}
		m_TradeSumarry.SetWindowTextA(m_tradePair_New->TradeSummary);
	}
}

void PairCalculatorDetail::UpdateDynamicValues()
{
	if(m_tradePair_New->Nature == 'B')
	{
		m_tradePair_New->ActualSpread = m_tradePair_New->Ratio * m_tradePair_New->BuyTrade->Price - m_tradePair_New->SellTrade->Price;
		if(m_tradePair_New->Ratio > 0)
		{
			m_tradePair_New->SellTrade->Qty = (int)(m_tradePair_New->BuyTrade->Qty / m_tradePair_New->Ratio);
		}
	}
	else
	{
		m_tradePair_New->ActualSpread = m_tradePair_New->Ratio * m_tradePair_New->SellTrade->Price - m_tradePair_New->BuyTrade->Price;
		if(m_tradePair_New->Ratio > 0)
		{
			m_tradePair_New->BuyTrade->Qty = (int)(m_tradePair_New->SellTrade->Qty / m_tradePair_New->Ratio);
		}
	}
	if(m_tradePair_New->Comparison == ">")
	{
		if(m_tradePair_New->ActualSpread > m_tradePair_New->RequiredSpred)
		{
			m_tradePair_New->Evalution = true;
		}
		else
		{
			m_tradePair_New->Evalution = false;
		}
	}
	else if(m_tradePair_New->Comparison == ">=")
	{
		if(m_tradePair_New->ActualSpread >= m_tradePair_New->RequiredSpred)
		{
			m_tradePair_New->Evalution = true;
		}
		else
		{
			m_tradePair_New->Evalution = false;
		}
	}
	else if(m_tradePair_New->Comparison == "<")
	{
		if(m_tradePair_New->ActualSpread < m_tradePair_New->RequiredSpred)
		{
			m_tradePair_New->Evalution = true;
		}
		else
		{
			m_tradePair_New->Evalution = false;
		}
	}
	else if(m_tradePair_New->Comparison == "=<")
	{
		if(m_tradePair_New->ActualSpread <= m_tradePair_New->RequiredSpred)
		{
			m_tradePair_New->Evalution = true;
		}
		else
		{
			m_tradePair_New->Evalution = false;
		}
	}
	else
	{
		m_tradePair_New->Evalution = false;
	}
}

void PairCalculatorDetail::OnTxtRatioChanged()
{
	CString ratio;
	ctrl_txtRatio.GetWindowText(ratio);
	if( ratio.IsEmpty() )
	{
		m_tradePair_New->Ratio = 0;
	}
	else
	{
		m_tradePair_New->Ratio = atof(ratio);
	}
	
}

void PairCalculatorDetail::OnTxtQtyChangeSymbol1()
{
	FillAll();
}

bool PairCalculatorDetail::CanCalculate()
{
	if(isFormClosing) return false;
	if(m_tradePair_New)
	{
		if(m_tradePair_New->BuyTrade)
		{
			if(m_tradePair_New->SellTrade)
			{
				return !(m_tradePair_New->BuyTrade->Stock.Name.IsEmpty() || m_tradePair_New->SellTrade->Stock.Name.IsEmpty());	
			}	
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	return false;
}

PairTrade* PairCalculatorDetail::GetTradePair()
{
	return m_tradePair_New;
}

void PairCalculatorDetail::OnPriceOnChangedSymbol1()
{
	FillAll();
	GetLivePrice();
}

void PairCalculatorDetail::OnPriceOnChangedSymbol2()
{
	FillAll();
	GetLivePrice();
}

void PairCalculatorDetail::OnOrderPriceOnChangedSymbol1()
{
	FillAll();
}

void PairCalculatorDetail::OnOrderPriceOnChangedSymbol2()
{
	FillAll();
}




void PairCalculatorDetail::OnCompareGTClicked()
{
	m_CompareSymbol = 0 ;
	FillAll();
}


void PairCalculatorDetail::OnCompareGTEClicked()
{
	m_CompareSymbol = 1 ;
	FillAll();
}


void PairCalculatorDetail::OnCompareLTClicked()
{
	m_CompareSymbol = 2 ;
	FillAll();
}


void PairCalculatorDetail::OnCompareLTEClicked()
{
	m_CompareSymbol = 3 ;
	FillAll();
}


void PairCalculatorDetail::OnTradeTypeBuyClicked()
{
	m_orderTradeType = 0 ;
	FillAll();
	GetLivePrice();
}


void PairCalculatorDetail::OnTradeTypeSellClicked()
{
	m_orderTradeType = 1 ;
	FillAll();
	GetLivePrice();
}

void PairCalculatorDetail::OnTxtRequiredSpreadChanged()
{
	CString requiredSpread;
	m_txtRequiredSpread.GetWindowText(requiredSpread);
	if( requiredSpread.IsEmpty() )
	{
		m_tradePair_New->RequiredSpred = 0;
	}
	else
	{
		m_tradePair_New->RequiredSpred = atof(requiredSpread);
	}
}


void PairCalculatorDetail::OnBnClickedCancel()
{
	isFormClosing = true;
	this->EndDialog(IDCANCEL);
}


void PairCalculatorDetail::OnMarketChangedSymbol1()
{
	FillAll();
}


void PairCalculatorDetail::OnMarketChangedSymbol2()
{
	FillAll();
}
